#ifndef __AT24CXX_H
#define __AT24CXX_H

#include "headfile.h"
#include "outputStream.h"
#include "I2C.h"

#define AT24Cxx_WRITE_ADDRESS_OUT (0xfffe)
#define AT24Cxx_WRITE_ERROR 0
#define AT24Cxx_READ_ADDRESS_OUT (0xfffe)
#define AT24Cxx_READ_ERROR (0xfffd)

typedef enum
{
    AT24Cxx_I2C_soft,
    AT24Cxx_I2C_hard
} AT24Cxx_I2C_mode;

class AT24Cxx : public outputStream
{
    I2C* i2c;
    uint8_t decive_addr;
    uint16_t write_addr;
    uint16_t read_addr;

public:
    AT24Cxx(I2C *i2c_class, uint8_t addr = 0xA0)
    {
        i2c = i2c_class;
        decive_addr = addr;
        write_addr = 0;
        read_addr = 0;
    };

    int begin()
    {
        i2c->begin();
        return check();
    }
    int end() { return 1; };

    int check();
    void setWriteAddr(uint16_t newaddr) { write_addr = newaddr; };
    void setReadAddr(uint16_t newaddr) { read_addr = newaddr; };

    virtual size_t write(uint8_t);
    virtual size_t write(const uint8_t *, size_t);
    size_t flush(uint8_t flush_data = 0)
    {
        uint8_t buf[256] = {};
        memset((void *)buf, flush_data, 256);
        write(buf, 256);
        write_addr = 0;
        return 0;
    };

    int16_t read();
    int16_t read(uint8_t *, size_t);
};

#endif
